
import React from 'react';
import {inject, observer} from 'mobx-react';
import {observable,action,toJS} from 'mobx';

// import {routerShape} from 'react-router';
import { WeaErrorPage, WeaTools } from 'ecCom'
import {
    WeaTop,
    WeaTab,
    WeaLeftRightLayout,
    WeaRightMenu
} from 'ecCom'
import LeftTab from './LeftTab';
import RoomPlanContainer from './RoomPlanContainer';
import {Button} from 'antd';
import Apis from '../../apis/roomPlan'
import '../../style/RoomPlan.less';
import {WeaLocaleProvider} from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;


@inject('meetingRoomPlan')
@inject('meetingCalView')
@inject('meetingDialog')
@observer
  class RoomPlan extends React.Component {
  constructor(props) {
		super(props);
		this.state={
			canEdit:false,
			leftVisible: false,
			rightBtns: [],
			WeaRightMenuProps: {
				collectParams: {
					favname: getLabel(15881,'会议室使用情况'),
					favouritetype: 5,
					objid: 0,
					importantlevel: 1,
				},

			}
		}
  }
  
  componentDidMount() {
  	/*const {meetingRoomPlan} = this.props;
    meetingRoomPlan.getRoomReportBaseData();*/
	let {meetingCalView}=this.props;
	let {canCreate}=this.props.meetingCalView;
	const { WeaRightMenuProps } = this.state
    const searchInfo = this.props.location.search ? this.props.location.search.split('?')[1]:''
    const search = searchInfo.length > 0 ?searchInfo.split('&'):[]
	let params = this.getQuery(search);
	const rightBtns = [];
	if(params.canEdit=='1'){
		rightBtns.push({ key: "new", content: getLabel(383430,"新建会议"), icon: <span className='icon-coms-New-Flow' /> })
	}
	this.setState({WeaRightMenuProps:{...WeaRightMenuProps,rightBtns:rightBtns, onClick: this.clickButton}, rightBtns:rightBtns})
	
    Apis.getRoomReportBaseData().then((result) => {
      if(!(result.hideCreateEntry&&result.hideCreateEntry == 1)){
        params&&params.canEdit?meetingCalView.changeCanCreate(params.canEdit=='1'?true:false):meetingCalView.changeCanCreate(false)
      }else{
        meetingCalView.changeCanCreate(false)
      }
	});
	if(window.document) {
		window.document.oncontextmenu = function(){ return false; };// 屏蔽鼠标右键默认行为
	}
  }
  
  getQuery = (search)=>{
    let params ={}
    search.forEach((v,index)=>{
      const tempV = v.split('=')
      params[tempV[0]] = tempV[1]
    });
    return params
  }

  componentWillReceiveProps(nextProps) {
		if(this.props.location.key !== nextProps.location.key) {
			let {meetingCalView}=this.props;
			const searchInfo = this.props.location.search ? this.props.location.search.split('?')[1]:''
			const search = searchInfo.length > 0 ?searchInfo.split('&'):[]
			let params = this.getQuery(search);
			Apis.getRoomReportBaseData().then((result) => {
				if(!(result.hideCreateEntry&&result.hideCreateEntry == 1)){
					params&&params.canEdit?meetingCalView.changeCanCreate(params.canEdit=='1'?true:false):meetingCalView.changeCanCreate(false)
				}else{
					meetingCalView.changeCanCreate(false)
				}
			})
			this.refs.roomCon && this.refs.roomCon.wrappedInstance.query();
		}
  }

  componentWillUnmount() {
		//组件卸载时一般清理一些状态
		const { roomPlanStatus: {org}, setOrg } = this.props.meetingRoomPlan;
		let params = {
			...org,
			id: ''
		};
		setOrg(params);
  }

  render() {
		let { query = false } = this.props.location;
		const { WeaRightMenuProps, rightBtns } = this.state
		let {roomPlanStatus } = this.props.meetingRoomPlan;
		let {canCreate}=this.props.meetingCalView;
		let { loading,  org } = toJS(roomPlanStatus);
		return (
			<div className = "metting-roomPlan">
				<WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@0d1plo`} datas={rightBtns} onClick={this.clickButton}  collectParams={WeaRightMenuProps.collectParams&&WeaRightMenuProps.collectParams}>
					<WeaTop ecId={`${this && this.props && this.props.ecId || ''}_WeaTop@iafpt9`}
						className={this.state.leftVisible ? '' : 'left-tree-visible-false'}
						title={WeaRightMenuProps.collectParams&&WeaRightMenuProps.collectParams.favname&&WeaRightMenuProps.collectParams.favname}
						loading={loading}
						icon={<i className='icon-coms-meeting' />}
						iconBgcolor='#f14a2d'
						buttons={[(canCreate&&<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@ki3hid@createMeeting`} type="primary" onClick={this.createMeeting}>{getLabel(383430,"新建会议")}</Button>),]}
						showDropIcon={true}
						dropMenuDatas={rightBtns}
						onDropMenuClick={this.clickButton}
						dropMenuProps={{collectParams: WeaRightMenuProps.collectParams&&WeaRightMenuProps.collectParams}}
					/>
				</WeaRightMenu>
				<div className="metting-roomPlan-content">
					<WeaLeftRightLayout ecId={`${this && this.props && this.props.ecId || ''}_WeaLeftRightLayout@y9nhfu`} onCollapse={(e)=>this.setState({leftVisible: e})} defaultShowLeft={false} leftCom={<LeftTab ecId={`${this && this.props && this.props.ecId || ''}_LeftTab@802kyb`} />} showLeft={this.state.leftVisible} leftWidth={260}>
						<div className="wea-right-container">
							{<RoomPlanContainer ecId={`${this && this.props && this.props.ecId || ''}_RoomPlanContainer@9ce6f9`} showLeftTree={this.showLeftTreeCallback} byWhat={query.byWhat} roomPlanType={query.mrtype} roomPlanId={query.roomid} ref="roomCon" subId={org.id} subName={org.name} 
							WeaRightMenuProps={WeaRightMenuProps}/>}
						</div>
					</WeaLeftRightLayout>
				</div>
				{/* </WeaTop> */}
			</div>
		)
  }

  showLeftTreeCallback=()=>{
	this.setState({leftVisible: !this.state.leftVisible});
  }
  
  showLeft = (flag) => {
		this.refs.roomCon && this.refs.roomCon.wrappedInstance.updateScroll();
	}

	createMeeting = () => {
		this.refs.roomCon && this.refs.roomCon.wrappedInstance.openCreateMeetingDialog();
		this.props.meetingDialog.setState({ meeetingDateVisible: false });
	}

	clickButton = (key) => {
		this.createMeeting();
	}

}

export default RoomPlan
